﻿using DX_Recognize.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZwSoft.ZwCAD.Runtime;

namespace DX_Recognize
{
    public partial class UserLoginForm : Form
    {
        public UserLoginForm()
        {
            InitializeComponent();
            try
            {
                string userInfoFile = @"D:\DX\ZW\UserInfo\UserInfo.json";
                Boolean F = System.IO.File.Exists(userInfoFile);
                if (F)
                {
                    string userInfoJson = File.ReadAllText(userInfoFile);
                    JObject userInfoJO = JObject.Parse(userInfoJson);
                    if(userInfoJO.ContainsKey("UserCount"))
                    {
                        string UserCount = (string)userInfoJO["UserCount"];
                        this.UserAccountText.Texts = UserCount;
                    }
                    if (userInfoJO.ContainsKey("UserName"))
                    {
                        string UserName = (string)userInfoJO["UserName"];
                        this.UserNameText.Texts = UserName;
                    }
                }

            }
            catch (System.Exception ex)
            {

            }
        }

        private void SureLoginBtn_Click(object sender, EventArgs e)
        {
            var account = this.UserAccountText.Texts;
            var username = this.UserNameText.Texts;
            Boolean F1 = !account.Trim().Equals("") && account != null;
            if (F1)
            {
                string userCountFolderPath = @"D:\DX\ZW\UserInfo";
                Boolean FF = System.IO.Directory.Exists(userCountFolderPath);
                if (!FF)
                    System.IO.Directory.CreateDirectory(userCountFolderPath);
                //往本地文件设置用户信息
                string userInfoFile = @"D:\DX\ZW\UserInfo\UserInfo.json";
                try
                {
                    Boolean F = System.IO.File.Exists(userInfoFile);
                    if (!F)
                    {
                        FileStream fileStream = File.Create(userInfoFile);
                        fileStream.Close();
                    }
                    UserAccountInfo userData = new UserAccountInfo();
                    userData.UserCount = account;
                    userData.UserName = username;
                    string UserJson = ConvertUtils.toJson(userData);
                    File.WriteAllText(userInfoFile, UserJson, System.Text.Encoding.UTF8);
                    MessageBox.Show("设置账户成功!");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("本地创建用户文件失败!");
                }
                finally
                {
                    this.Hide();
                }
            }
            else
                this.Hide();
        }

        private void QuitLoginBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        public class UserAccountInfo
        {
            public string UserCount { get; set; }
            public string UserName { get; set; }
        }

        private void UserLoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
        }
    }
}
